package com.leaf.spring.y202002.day11.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Client {

	public static void main(String[] args) {

		// 创建连接的地址
		InetSocketAddress address = new InetSocketAddress("127.0.0.1", 5678);

		// 建立缓冲区
		ByteBuffer buf = ByteBuffer.allocate(1024);

		// 声明连接的通道
		// 打开通道
		try (SocketChannel sc = SocketChannel.open()) {

			// 进行连接
			sc.connect(address);

			while (true) {
				byte[] bytes = new byte[1024];

				System.in.read(bytes);

				buf.put(bytes);
				//对缓冲区进行复位
				buf.flip();
				//写出数据
				sc.write(buf);
				//清空缓冲区
				buf.clear();

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
